package com.core.annotation.concept;

/**
 *
 * @author Sunil
 */
class Stack {
    
    int x;
    boolean flag = false;
    
    public synchronized void push(int x) {
        if (flag) {
            try {
                wait();
            } catch (Exception e) {
            }
        }
        this.x = x;
        System.out.println(x + "is pushed");
        flag=true;
        notify();
    }
    
    public synchronized int pop() {
        if (!flag) {
            try {
                wait();
            } catch (Exception e) {
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        }
        System.out.println(x + " is pop");
        flag=false;
        notify();
        return x;
    }
}

class Producer implements Runnable {
    
    Stack st = null;
    
    public Producer(String name, Stack st) {
        this.st = st;
        Thread t1 = new Thread(this, name);
        t1.start();
    }
    
    public void run() {
        int a = 1;
        for (int i = 0; i < 7; i++) {
            st.push(a++);
        }
    }
}

class Consumer implements Runnable {
    
    Stack st = null;
    
    public Consumer(String name, Stack st) {
        this.st = st;
        Thread t2 = new Thread(this, name);
        t2.start();
    }
    
    public void run() {
        for (int i = 0; i < 7; i++) {
            st.pop();
        }
    }
}

public class MultTest {

    public static void main(String[] args) {
        Stack st = new Stack();
        Producer p = new Producer("proc", st);
        Consumer c = new Consumer("cons", st);
    }
}
